<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8" isELIgnored="false"%>

<%
	request.setAttribute("title","货品信息");
	request.setAttribute("modelConfigPath", "cargo");
%>

<%@ include file="/WEB-INF/views/shared/master/contentHeader.jsp"%>
<style type="text/css">
.hot-container{
overflow: auto;
}

.colHeader{
word-wrap:break-word;white-space:normal
}
td{
word-break:break-all;
}

</style>
<t:panelFoot headingBtnCssClass="heading-btn pull-right-important">
	<t:button icon="icon-floppy-disk" onclick="saveForm()" cssClass="btn-info" csize="sm">保存</t:button>
	<span style="color:red;margin-left:20px;" class="aaa"></span>
</t:panelFoot>
<t:panel cssClass="panel-flat form-detailed" customBody="true" customHead="true">

<input type="hidden" value="${productVersionId}" id="productVersionId" name="productVersionId"/>
	<div class="detailed-navbar-content panel-body">
		<div>
			<div class="hot-container">
				<div id="hot_parameter_info"></div>
			</div>
		</div>
	</div>
</t:panel>

<script type="text/javascript">
	function createSpreadsheetData(rowCount, colCount) {
		rowCount = typeof rowCount === 'number' ? rowCount : 100;
		colCount = typeof colCount === 'number' ? colCount : 4;
		var rows = [], i, j;
		for (i = 0; i < rowCount; i++) {
			var row = [];
			for (j = 0; j < colCount; j++) {
				row.push('');
			}
			rows.push(row);
		}
		return rows;
	}
	var $hot=null;
	$(function() {
		initTable();
		reload();
		setTimeout(function(){
			$(".panel-body").click();	
		},1000);
	});
	
	var empty=function(value, callback){
		setTimeout(function(){
			callback(!$.isNull(value));	
		},10);
	};
	
	
	function initTable(){
		
		var hot_parameter_info = document.getElementById('hot_parameter_info');
		$hot = new Handsontable(hot_parameter_info, {
			rowHeaders : true,
			autoColumnSize:true,
		
			stretchH:'all',
			contextMenu: ['row_above', 'row_below', 'remove_row'],
			colHeaders : [ '货品编号', '货品名称', '规格', "属性", "货品分类", "价格",
			               "备注","描述"],
			colWidths:[100,100,100,100,50,50,50,100,100,100,100,50,50,50,100,100,100,100,50,50,50,50,50,50],
			columns: [{data:'cargoNo',validator:empty},
			           {data:'cargoName'},
			           {data:'standard'},
			           {data:'standardProerty'},
			           {data:'category'},
			           {data:'price'},
			           {data:'comment'},
			           {data:'describes'},
			         ]
		});

		$hot.updateSettings({
			contextMenu : {
				items : {
					"row_above" : {name : '在上面添加行'},
					"row_below" : {name : '在下面添加行'},
					"remove_row" : {name : '删除当前行'}
				}
			}
		});
	}
	
	
	function loadTable(){
		return getEmptyData();
	
	}
	
	function getEmptyData() {
		
		var hot_borders_data = createSpreadsheetData(10, 8);
		return hot_borders_data;
	}
	
	function reload(){
		var data=loadTable();
		$hot.loadData(data);
		$hot.render();
	}
	
	function saveForm(){
		var productVersionId=$("#productVersionId").val();
		deleteEmpty();
		
		$hot.validateCells(function(result){
			if(!result){
				$.topNotify("表格有错误信息，检查表格红色区域","error");
				return;
			}
			var parameterInfos=getDataJson();
			var productNoJson=null;
			var productNos=getProductNoJson();
			
			if($.isNull(parameterInfos)){
				$.topNotify("表格无内容","error");
				return;
			}
			var urltest=$.getVirtualPath()+"/admin/product/parameterInfo/validate?productNos="+productNos;
			var str=$.getTextPost(urltest);
			productNoJson =JSON.parse(str);
			if(productNoJson.length==0){
				var url=$.getVirtualPath()+"/admin/product/parameterInfo/saves?productVersionId="+productVersionId;
				$.ajax({
					url:url,
					type:'post',
					dataType:'json',
					contentType:"application/json",
					data:JSON.stringify(parameterInfos),
					success:function(result){
						top.main.parameterInfoIframe.formSubmit();
						closeModal();
						$.topNotify(result.message,result.resultType);	
					}
				});
			}else{
				
				$(".aaa").text("产品编号"+productNoJson+"已存在,请勿重复录入");
			}
			
		});
		
	}
	
	function getProductNoJson(){
		var rows=$hot.getSourceData();
		var columns=[];
		for(var i=0;i<rows.length;i++){
			var data=rows[i];
			columns.push(data.productNo);
		}
		
		return columns.join(",");
		
	}
	
	function getDataJson(){
		var rows=$hot.getSourceData();
		var columns=[];
		for(var i=0;i<rows.length;i++){
			var data=rows[i];
			columns.push({"productNo":data.productNo,
						  "customerNo":data.customerNo,
						  "setoverET":data.setoverET,
						  "pcd":data.pcd,
						  "centreHoleD":data.centreHoleD,
						  "pcdCode":data.pcdCode,
						  "ph":data.ph,
						  "zx45":data.zx45,
						  "productSize":data.productSize,
						  "boltHoleStructure":data.boltHoleStructure,
						  "backDistanceF":data.backDistanceF,
						  "subpanelDiameterD1":data.subpanelDiameterD1,
						  "subpanelPlyH":data.subpanelPlyH,
						  "overlay":data.overlay,
						  "overlayBolt":data.overlayBolt,
						  "overlayBoltNo":data.overlayBoltNo,
						  "processFlow":data.processFlow,
						  "surfaceState":data.surfaceState,
						  "blankNo":data.blankNo,
						  "beforehandDrillDiameterD3":data.beforehandDrillDiameterD3,
						  "backDistanceF1":data.backDistanceF1,
						  "subpanelDiameterD2":data.subpanelDiameterD2,
						  "moldCoreET":data.moldCoreET,
						  "applyVehicleType":data.applyVehicleType});
		}
		return columns;
	}
	
	//删除空行
	function deleteEmpty(){
		var rows=$hot.countRows();
		for(var i=rows-1;i>=0;i--){
			if($hot.isEmptyRow(i)){
				$hot.alter('remove_row', i);
			}
		}
	}
	
</script>

<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/handsontable.jsp"%>
<%@ include file="/WEB-INF/views/shared/scripts/editor/ueditor.jsp"%>
<%@ include file="/WEB-INF/views/shared/master/dialog.jsp"%>
<%@ include file="/WEB-INF/views/shared/master/contentFooter.jsp"%>
